from datetime import datetime
from fastapi import APIRouter, Depends, Form, Request
from pydantic_validation_decorator import ValidateFields
from sqlalchemy.ext.asyncio import AsyncSession
from config.enums import BusinessType
from config.get_db import get_db
from module_admin.annotation.log_annotation import Log
from module_admin.aspect.interface_auth import CheckUserInterfaceAuth
from module_admin.entity.vo.user_vo import CurrentUserModel
from module_admin.service.login_service import LoginService
from module_rag.service.rag_service import RagService
from module_rag.entity.vo.rag_vo import DeleteRagModel, RagModel, RagPageQueryModel
from utils.common_util import bytes2file_response
from utils.log_util import logger
from utils.page_util import PageResponseModel
from utils.response_util import ResponseUtil


ragController = APIRouter(prefix='/manage/rag', dependencies=[Depends(LoginService.get_current_user)])


@ragController.get(
    '/list', response_model=PageResponseModel, dependencies=[Depends(CheckUserInterfaceAuth('manage:rag:list'))]
)
async def get_manage_rag_list(
    request: Request,
rag_page_query: RagPageQueryModel = Depends(RagPageQueryModel.as_query),
    query_db: AsyncSession = Depends(get_db),
):
    # 获取分页数据
    rag_page_query_result = await RagService.get_rag_list_services(query_db, rag_page_query, is_page=True)
    logger.info('获取成功')

    return ResponseUtil.success(model_content=rag_page_query_result)


@ragController.post('', dependencies=[Depends(CheckUserInterfaceAuth('manage:rag:add'))])
@ValidateFields(validate_model='add_rag')
@Log(title='知识库信息', business_type=BusinessType.INSERT)
async def add_manage_rag(
    request: Request,
    add_rag: RagModel,
    query_db: AsyncSession = Depends(get_db),
    current_user: CurrentUserModel = Depends(LoginService.get_current_user),
):
    add_rag.create_time = datetime.now()
    add_rag.create_by = current_user.user.user_name
    add_rag.update_time = datetime.now()
    add_rag.update_by = current_user.user.user_name
    add_rag_result = await RagService.add_rag_services(query_db, add_rag)
    logger.info(add_rag_result.message)

    return ResponseUtil.success(msg=add_rag_result.message)


@ragController.put('', dependencies=[Depends(CheckUserInterfaceAuth('manage:rag:edit'))])
@ValidateFields(validate_model='edit_rag')
@Log(title='知识库信息', business_type=BusinessType.UPDATE)
async def edit_manage_rag(
    request: Request,
    edit_rag: RagModel,
    query_db: AsyncSession = Depends(get_db),
    current_user: CurrentUserModel = Depends(LoginService.get_current_user),
):
    edit_rag.update_by = current_user.user.user_name
    edit_rag.update_time = datetime.now()
    edit_rag_result = await RagService.edit_rag_services(query_db, edit_rag)
    logger.info(edit_rag_result.message)

    return ResponseUtil.success(msg=edit_rag_result.message)


@ragController.delete('/{ids}', dependencies=[Depends(CheckUserInterfaceAuth('manage:rag:remove'))])
@Log(title='知识库信息', business_type=BusinessType.DELETE)
async def delete_manage_rag(request: Request, ids: str, query_db: AsyncSession = Depends(get_db)):
    delete_rag = DeleteRagModel(ids=ids)
    delete_rag_result = await RagService.delete_rag_services(query_db, delete_rag)
    logger.info(delete_rag_result.message)

    return ResponseUtil.success(msg=delete_rag_result.message)


@ragController.get(
    '/{id}', response_model=RagModel, dependencies=[Depends(CheckUserInterfaceAuth('manage:rag:query'))]
)
async def query_detail_manage_rag(request: Request, id: int, query_db: AsyncSession = Depends(get_db)):
    rag_detail_result = await RagService.rag_detail_services(query_db, id)
    logger.info(f'获取id为{id}的信息成功')

    return ResponseUtil.success(data=rag_detail_result)


@ragController.post('/export', dependencies=[Depends(CheckUserInterfaceAuth('manage:rag:export'))])
@Log(title='知识库信息', business_type=BusinessType.EXPORT)
async def export_manage_rag_list(
    request: Request,
    rag_page_query: RagPageQueryModel = Form(),
    query_db: AsyncSession = Depends(get_db),
):
    # 获取全量数据
    rag_query_result = await RagService.get_rag_list_services(query_db, rag_page_query, is_page=False)
    rag_export_result = await RagService.export_rag_list_services(request, rag_query_result)
    logger.info('导出成功')

    return ResponseUtil.streaming(data=bytes2file_response(rag_export_result))
